/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Produccion;

import Controlador.Bean.Produccion.ProduccionRealRB;
import gui.Almacen.DetalleGestionAlmacen;
import gui.General.Main;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.beans.PropertyVetoException;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author ELECTRO
 */
public class buscarProduccion extends javax.swing.JPanel {

    /**
     * Creates new form buscarProduccion
     */
    
    
    private TableModel tb;
    int clic=0;
    String selectedId;
    
    public buscarProduccion() {
        initComponents();
           initComp();
    }
    
    private void initComp(){
         tb = new TableModel();
         tblProductos1.setModel(tb);
         tblProductos1.setRowSelectionAllowed(true);
         tblProductos1.setDefaultRenderer(JButton.class, new MiRender());
         tblProductos1.setDefaultEditor( JButton.class, new MiEditor());
     }
    
    String id  = "";
    String nombre = "";
    public void filtrar(String id, String nombre){
        this.id = id;
        this.nombre = nombre;
        tb.actualizarTabla();
    }
        class TableModel extends AbstractTableModel{
              
        //Two arrays used for the table data
        String[] columnNames = {"Código Producción", "Nombre TipoMáquina", "Turno"
                        , "Fecha Producción", "Apellido Paterno" , "Apellido Materno"
                , "Nombre", "Id Producto" , "Cantidad Producida", "Merma" };
               
        public List<ProduccionRealRB> data = Controlador.Service.Produccion.GestionProduccion.obtenerProduccionRB();
        
        public void actualizarTabla(){
            data = Controlador.Service.Produccion.GestionProduccion.filtrarProduccion(id, nombre);
            fireTableDataChanged();
            id="";
            nombre="";
        }
        
        
        @Override
        public int getRowCount()
        {
            return data.size();
        }
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {        
            switch(column){
                case 0: return data.get(row).getIdProduccion();
                case 1: return data.get(row).getNombreM(); 
                case 2: return data.get(row).getIdTurno();
                case 3: return data.get(row).getFecha();
                case 4: return data.get(row).getApellidoP();
                case 5: return data.get(row).getApellidoM();
                case 6: return data.get(row).getNombreO();
                case 7: return data.get(row).getCodProducto();
                case 8: return data.get(row).getProduccion();
                default: return data.get(row).getMerma();
                    
            }    
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
            if(c!=10){
            return getValueAt(0, c).getClass();
            }else{
                return JButton.class;
            }
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
           
	 
		return true;
	   }
        }
        
public class MiRender implements TableCellRenderer {
        // Este metodo indica como debe de pintarse el elemento en la fila row, en la columna column
        // que esta en la tabla table y tiene el color dado por el objeto.
        @Override
        public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {    
            // como el boton y el calendario son componentes esto se vale.
            return (JComponent)table.getValueAt(row, column);
        }
     }
     
     public class MiEditor extends AbstractCellEditor implements TableCellEditor {
         private Boolean currentValue;
         @Override
         public Object getCellEditorValue()
         {
            return currentValue;
         }
         //El editor usara el propio componente. Para que funcione la celda en el modelo debe ser editable.
         @Override
         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
         {
           return (JComponent) value;// la tabla solo debe tener componentes graficos
         }
     }
    
    
  
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblProductos1 = new javax.swing.JTable();
        btnSalida = new javax.swing.JButton();
        pnlCamposBusqueda = new javax.swing.JPanel();
        lblDestino = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        txtCodigo = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        lblDestino1 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        tblProductos1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblProductos1.setMinimumSize(new java.awt.Dimension(60, 20));
        tblProductos1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblProductos1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblProductos1);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        btnSalida.setText("Registro Producción");
        btnSalida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalidaActionPerformed(evt);
            }
        });

        pnlCamposBusqueda.setBorder(javax.swing.BorderFactory.createTitledBorder("Campos de búsqueda"));

        lblDestino.setText("IdProducto:");

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Limpiar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblDestino1.setText("Nombre Empleado:");

        javax.swing.GroupLayout pnlCamposBusquedaLayout = new javax.swing.GroupLayout(pnlCamposBusqueda);
        pnlCamposBusqueda.setLayout(pnlCamposBusquedaLayout);
        pnlCamposBusquedaLayout.setHorizontalGroup(
            pnlCamposBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCamposBusquedaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlCamposBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCamposBusquedaLayout.createSequentialGroup()
                        .addComponent(btnBuscar)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar)
                        .addGap(293, 293, 293))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCamposBusquedaLayout.createSequentialGroup()
                        .addComponent(lblDestino)
                        .addGap(18, 18, 18)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(94, 94, 94)
                        .addComponent(lblDestino1)
                        .addGap(29, 29, 29)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25))))
        );
        pnlCamposBusquedaLayout.setVerticalGroup(
            pnlCamposBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCamposBusquedaLayout.createSequentialGroup()
                .addContainerGap(12, Short.MAX_VALUE)
                .addGroup(pnlCamposBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlCamposBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblDestino)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlCamposBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblDestino1)
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlCamposBusquedaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(btnCancelar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(btnSalida)
                        .addContainerGap(614, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pnlCamposBusqueda, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnSalida)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlCamposBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalidaActionPerformed
        // TODO add your handling code here:
        agregarProduccion apro = new agregarProduccion();
        Main.getDesktop().add(apro);
        apro.setVisible(true);
    }//GEN-LAST:event_btnSalidaActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        String id = txtCodigo.getText();
        String nombre = txtNombre.getText();
        filtrar(id,nombre);
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void tblProductos1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblProductos1MouseClicked
        // TODO add your handling code here:

        int selectedRowIndex = tblProductos1.getSelectedRow();
        int selectedColumnIndex = tblProductos1.getSelectedColumn();
        if(selectedColumnIndex!=10){
            String selectId = (String) tblProductos1.getValueAt(selectedRowIndex, 0);
            if(clic == 1 && selectedId.compareTo(selectId)==0){
                //                txtIdProducto.setText((String)tblProductos.getValueAt(selectedRowIndex, 0));
                //                txtNombre.setText((String)tblProductos.getValueAt(selectedRowIndex, 1));
                //                txtDescripcion.setText((String)tblProductos.getValueAt(selectedRowIndex, 2));
                //                txtStockMinimo.setText(((Double)tblProductos.getValueAt(selectedRowIndex, 3)).toString());
                //                txtCantidad.setText(((Double)tblProductos.getValueAt(selectedRowIndex, 4)).toString());
                //                cmbTipo.setSelectedItem(tblProductos.getValueAt(selectedRowIndex, 5));
                //                cmbTipo.setSelectedIndex(Integer.parseInt((String)tblProductos.getValueAt(selectedRowIndex, 6)));
                clic = 0;
                //                cmbTipo.setEnabled(false);
            }else
            if(clic == 0){
                selectedId = selectId;
                clic++;
            }else{
                clic = 0;
            }
        }else{
            //            agregarMasa  am = new agregarMasa((String)tblProductos.getValueAt(selectedRowIndex, 0));

//            int confirmado = JOptionPane.showConfirmDialog(this,"¿Desea seleccionar el producto?");
//            if (confirmado == JOptionPane.OK_OPTION){
//                agregarMasa  am = new agregarMasa((String)tblProductos.getValueAt(selectedRowIndex, 0));
//                Main.getDesktop().add(am);
//                am.setVisible(true);
//                this.dispose();
//                //                Controlador.Service.Almacen.GestionAlmacenes.eliminarProductos((String)tblProductos.getValueAt(selectedRowIndex, 0));
//                //                tb.actualizarTabla();
//            }
        }
    }//GEN-LAST:event_tblProductos1MouseClicked

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        txtNombre.setText("");
        txtCodigo.setText("");
        filtrar("","");
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSalida;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblDestino;
    private javax.swing.JLabel lblDestino1;
    private javax.swing.JPanel pnlCamposBusqueda;
    private javax.swing.JTable tblProductos1;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
